﻿var Ie = {
    version: '1.0.0.0.0',
    PageIndex: 1,
    PageSize: 20,

    ListSearch: function (pageIndex) {
        $('#areaIeDetail').css('display', 'none');

        $('#lstIe tbody').html('<tr><td colspan="6" class="aligncenter"><img src="/images/loading.gif" alt=""/></td></tr>');

        $('#toTop').click();

        var keyword = $('#txtKeyword').val();
        var startDate = $('#txtStartDate').val();
        var endDate = $('#txtEndDate').val();

        var typeId = 0;
        $.each($('#ddlType ul li a'), function () {
            if ($(this).attr('class') == 'selected') {
                var index = $(this).attr('index');
                var value = $('#ddlType select.jqTransformHidden option:eq(' + index + ')').attr('value');
                if (value > 0 && value != undefined)
                    typeId = value;
            }
        });

        var storeId = 0;
        $.each($('#ddlStore ul li a'), function () {
            if ($(this).attr('class') == 'selected') {
                var index = $(this).attr('index');
                var value = $('#ddlStore select.jqTransformHidden option:eq(' + index + ')').attr('value');
                storeId = value;
            }
        });

        var url = String.format('/GetRequest.ashx?m=ie&action=listsearch&keyword={0}&tId={1}&storeId={2}&sd={3}&ed={4}&pi={5}&ps={6}&rand={7}',
                    keyword, typeId, storeId, startDate, endDate, pageIndex, Ie.PageSize, Math.random());

        $.getJSON(url, function (res) {
            if (res != null) {
                $('.pages').html(res.Pager);

                $.Mustache.load('/templates/ie/list.htm')
                    .done(function () {
                        $('#lstIe tbody').html('');
                        $('#lstIe tbody').mustache('tpl-ie-list', res);
                        $('.topDir').tipsy({ fade: true, gravity: 's' });
                        $('form').jqTransform();
                    });
            }
        });
    },

    Delete: function (id) {
        $('#lstIe table tr').removeClass('gradeC');
        $('#ie-' + id).addClass('gradeC');

        jConfirm('', 'Bạn có chắc chắn muốn xóa?', function (result) {
            if (result) {
                var url = String.format('/GetRequest.ashx?m=ie&action=delete&ieId={0}', id);
                $.post(url, function () { })
                .done(function () {
                    $.jGrowl(String.format('Xóa dữ liệu thành công!'), { header: 'Thông báo:' });
                    Ie.ListSearch(Ie.PageIndex);
                });
            }
        });
    },

    ReadExcel: function () {

        var storeId = 0;
        $.each($('#ddlStore ul li a'), function () {
            if ($(this).attr('class') == 'selected') {
                var index = $(this).attr('index');
                var value = $('#ddlStore select.jqTransformHidden option:eq(' + index + ')').attr('value');
                storeId = value;
            }
        });
        
        var typeId = 0;
        $.each($('#ddlType ul li a'), function () {
            if ($(this).attr('class') == 'selected') {
                var index = $(this).attr('index');
                var value = $('#ddlType select.jqTransformHidden option:eq(' + index + ')').attr('value');
                if (value > 0 && value != undefined)
                    typeId = value;
            }
        });

        var filePath = $('#txtFilePath').val();
        var desc = $('#txtDescription').val();

        var url = String.format('/GetRequest.ashx?m=ie&action=readexcel&tId={0}&storeId={1}&desc={2}&filePath={3}', typeId, storeId, desc, filePath);

        $.ajax({
            type: "POST",
            url: url,
            dataType: 'json'
        }).done(function (res) {

            $('#excelArea').css('display', 'block');

            if (res.Success) {
                $.Mustache.load('/templates/excel/ie.htm')
                    .done(function () {
                        $('#lstProduct_ImportExport tbody').html('');
                        $('#lstProduct_ImportExport tbody').mustache('tpl-excel-import-export-list', res);
                        $('#btnSave').css('display', 'block');
                    });
            }
            else {
                $('#lstProduct_ImportExport tbody').html(res.Message);
            }
        });

    },

    ExecuteExcel: function () {
        var storeId = 0;
        $.each($('#ddlStore ul li a'), function () {
            if ($(this).attr('class') == 'selected') {
                var index = $(this).attr('index');
                var value = $('#ddlStore select.jqTransformHidden option:eq(' + index + ')').attr('value');
                storeId = value;
            }
        });

        var typeId = 0;
        $.each($('#ddlType ul li a'), function () {
            if ($(this).attr('class') == 'selected') {
                var index = $(this).attr('index');
                var value = $('#ddlType select.jqTransformHidden option:eq(' + index + ')').attr('value');
                if (value > 0 && value != undefined)
                    typeId = value;
            }
        });

        var filePath = $('#txtFilePath').val();
        var desc = $('#txtDescription').val();

        var url = String.format('/GetRequest.ashx?m=ie&action=executeexcel&tId={0}&storeId={1}&desc={2}&filePath={3}', typeId, storeId, desc, filePath);
       
        $.ajax({
            type: "POST",
            url: url,
            dataType: 'json',
            timeout: 3600
        }).done(function (res) {
            var html = '';

            if (res.Success) {
                html += '<div class="nNote nSuccess hideit">';
                html += String.format('<p><strong>Thành công: </strong>{0}</p>', res.Message);
                html += '</div>';
                $('#excelArea').css('display', 'none');
                $('#message').html(html);
            }
            else {
                html += '<div class="nNote nFailure hideit">';
                html += String.format('<p><strong>Lỗi: </strong>{0}</p>', res.Message);
                html += '</div>';
                $('#excelArea').css('display', 'none');
                $('#message').html(html);
            }
        });
    }
};    